//
//  BirthNoticeHeadView.m
//  fitnessmanage
//
//  Created by wujian on 2018/1/12.
//  Copyright © 2018年 zongquankeji. All rights reserved.
//

#import "BirthNoticeHeadView.h"

@interface BirthNoticeHeadView()

@end

@implementation BirthNoticeHeadView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        /**
         搜索
         */
        SearchView *searchView = [[SearchView alloc] init];
        searchView.placeholder = @"请输入会员名、会员卡号、手机";
        searchView.backgroundColor = CSWhiteColor;
        [self addSubview:searchView];
        self.searchView = searchView;
        
        /**
         CollectionView
         */
        UICollectionViewFlowLayout *collectViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
        collectViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        //设置cell之间间距
        collectViewFlowLayout.minimumInteritemSpacing = 10;
        //设置行距
        collectViewFlowLayout.minimumLineSpacing = 0;
        collectViewFlowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
        CGFloat itemWith = (SCREEN_WIDTH - 20) / 7;
        //设置Item大小
        collectViewFlowLayout.itemSize = CGSizeMake(itemWith, 30);
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:collectViewFlowLayout];
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.backgroundColor = CSWhiteColor;
        collectionView.delegate = self;
        collectionView.dataSource = self;
        [collectionView registerClass:[BirthNoticeCollectionViewCell class] forCellWithReuseIdentifier:@"BirthNoticeCollectionViewCell"];
        [self addSubview:collectionView];
        self.collectionView = collectionView;
        
        UIImageView *birthImageView = [[UIImageView alloc] init];
        birthImageView.image = [UIImage imageNamed:@"img_birthdayrem"];
        [self addSubview:birthImageView];
        self.birthImageView = birthImageView;
        
        
        UILabel *birthLabel = [[UILabel alloc] init];
        birthLabel.text = @"当月生日共：";
        birthLabel.font = MidFont;
        birthLabel.textColor = CSColorFromRGB(0x666666);
        [birthImageView addSubview:birthLabel];
        self.birthLabel = birthLabel;
        
        UILabel *birthCountLabel = [[UILabel alloc] init];
        birthCountLabel.text = @"0";
        birthCountLabel.textColor = CSColorFromRGB(0x0a0a0a);
        birthCountLabel.font = MidFont;
        [birthImageView addSubview:birthCountLabel];
        self.birthCountLabel = birthCountLabel;
        
        
        UILabel *birthPeopleLabel = [[UILabel alloc] init];
        birthPeopleLabel.font = MidFont;
        birthPeopleLabel.textColor = CSColorFromRGB(0x666666);
        birthPeopleLabel.text = @"人";
        [birthImageView addSubview:birthPeopleLabel];
        self.birthPeopleLabel = birthPeopleLabel;
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    /**
     搜索
     */
    [self.searchView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).offset(10);
        
        make.left.mas_equalTo(self).offset(20);
        
        make.right.mas_equalTo(self).offset(-20);
        
        make.height.mas_equalTo(40);
    }];
    
    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.searchView.mas_bottom).offset(10);
        
        make.left.width.mas_equalTo(self);
        
        make.height.mas_equalTo(60);
    }];
    
    [self.birthImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.collectionView.mas_bottom).offset(15);
        
        make.left.mas_equalTo(self).offset(25);
        
        make.bottom.mas_equalTo(self).offset(-10);
    }];
    
    [self.birthLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
 
        make.centerY.mas_equalTo(self.birthImageView).offset(3);
        
        make.left.mas_equalTo(self.birthImageView).offset(60);
    }];
    
    [self.birthCountLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.birthLabel);
        
        make.left.mas_equalTo(self.birthLabel.mas_right);
    }];
    
    [self.birthPeopleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.birthLabel);
        
        make.left.mas_equalTo(self.birthCountLabel.mas_right).offset(1);
    }];
}

#pragma mark UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 12;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    BirthNoticeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BirthNoticeCollectionViewCell" forIndexPath:indexPath];
    
    cell.titleLabel.text = [NSString stringWithFormat:@"%ld月",indexPath.row + 1];
    
    if (self.selectRow == indexPath.row) {
        
        cell.backgroundColor = ButtonBackGroundColor;
        
        cell.layer.cornerRadius = 2;
        
        cell.layer.masksToBounds = YES;
    }
    else{
        
        cell.backgroundColor = CSWhiteColor;
    }
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    self.selectRow = indexPath.row;
    
    [self.collectionView reloadData];
    
    if (self.sendSelectBirthBlock) {
        
        self.sendSelectBirthBlock(self.selectRow);
    }
}


-(void)setSelectRow:(NSInteger)selectRow{
    
    _selectRow = selectRow;
    
    [self.collectionView reloadData];
}
@end
